﻿Public Class EditUser



    ''' <summary>
    ''' 注册
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If String.IsNullOrEmpty(txbUserName.Text.Trim) Then
            MessageBox.Show("姓名不能为空")
            Return
        ElseIf String.IsNullOrEmpty(txbpwd.Text) Then
            MessageBox.Show("密码不能为空")
            Return
        ElseIf String.IsNullOrEmpty(txbpwd2.Text) Then
            MessageBox.Show("确认密码不能为空")
            Return
        ElseIf txbpwd.Text.Trim <> txbpwd2.Text.Trim Then
            MessageBox.Show("密码输入不一致")
            txbpwd.Text = ""
            txbpwd2.Text = ""
            Return
        End If


        Dim user As New UserInfo
        'MessageBox.Show("开始注册")
        user.UserName = txbUserName.Text.Trim
        user.PassWord = txbpwd.Text.Trim
        user.Address = txbaddress.Text.Trim
        user.BirthTime = DateTimePicker1.Value.ToString("yyyy-MM-dd")
        If cbbRole.SelectedItem Is Nothing Then
            user.role = ""
        Else
            user.role = cbbRole.SelectedItem.ToString
        End If

        If CbbSex.SelectedItem Is Nothing Then
            user.Sex = ""
        Else
            user.Sex = CbbSex.SelectedItem.ToString
        End If


        user.State = "1"

        If UserBLL.Register(user) Then
            MessageBox.Show("添加成功")
            Dim parentForm As MainForm
            parentForm = Me.Owner
            parentForm.SetDataGridView()

            Me.Close()
            Me.Dispose()

        Else
            MessageBox.Show("添加失败")
        End If

    End Sub

    ''' <summary>
    ''' 修改
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If String.IsNullOrEmpty(txbUserName.Text.Trim) Then
            MessageBox.Show("姓名不能为空")
            Return
        End If

        If Not String.IsNullOrEmpty(txbpwd.Text.Trim) Or Not String.IsNullOrEmpty(txbpwd2.Text.Trim) Then
            If String.IsNullOrEmpty(txbpwd.Text) Then
                MessageBox.Show("密码不能为空")
                Return
            ElseIf String.IsNullOrEmpty(txbpwd2.Text) Then
                MessageBox.Show("确认密码不能为空")
                Return
            ElseIf txbpwd.Text.Trim <> txbpwd2.Text.Trim Then
                MessageBox.Show("密码输入不一致")
                txbpwd.Text = ""
                txbpwd2.Text = ""
                Return
            End If
        End If



            Dim user As New UserInfo

            user.ID = lblD.Text.Substring(lblD.Text.LastIndexOf(":") + 1)
            user.UserName = txbUserName.Text.Trim
            user.PassWord = txbpwd.Text.Trim
            user.Address = txbaddress.Text.Trim
            user.BirthTime = DateTimePicker1.Value.ToString("yyyy-MM-dd")
            If cbbRole.SelectedItem Is Nothing Then
                user.role = ""
            Else
                user.role = cbbRole.SelectedItem.ToString
            End If

            If CbbSex.SelectedItem Is Nothing Then
                user.Sex = ""
            Else
                user.Sex = CbbSex.SelectedItem.ToString
            End If


            user.State = "1"

            If UserBLL.UpdataUser(user) Then
                MessageBox.Show("修改成功")
                Dim parentForm As MainForm
                parentForm = Me.Owner
                parentForm.SetDataGridView()

                Me.Close()
                Me.Dispose()

            Else
                MessageBox.Show("添加失败")
            End If


    End Sub

    Public Sub InitData(ByVal id As String)
        Dim user As UserInfo
        user = UserBLL.GetUserInfoByID(id)

        txbUserName.Text = user.UserName
        txbpwd.Text = user.PassWord
        txbaddress.Text = user.Address
        DateTimePicker1.Value = user.BirthTime
        cbbRole.SelectedItem = user.role
        CbbSex.SelectedItem = user.Sex
        lblD.Text = "用户ID: " + user.ID.ToString
        lblD.Visible = True


    End Sub
End Class